﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using System.Security.Cryptography;
using System.IO.Ports;
using System.Threading;
using NPOI.SS.Formula.Functions;
using static CoreShop.App.FOrdering;
using System.Data.SqlTypes;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.App
{
    public partial class frmFoodSpecWeigh : UIForm
    {
        private corecmsfoodordering ordering = null;
        private corecmsfoodspec foodspec = null;
        public decimal mScaleQty = 0;
        //private bool bGetWeighQty = false;
        public delegate void SetWeighQty(string data);
        public SetWeighQty setWeighQty;
        public System.Timers.Timer timer_focus = null;

        public frmFoodSpecWeigh(corecmsfoodordering _ordering, corecmsfoodspec _foodspec,bool _getWeighFlag = false)
        {
            ordering = _ordering;
            foodspec = _foodspec;                
            InitializeComponent();
            lblScaleQty.TextBox.GotFocus += txtQuantity_GotFocus;
            if ((_getWeighFlag) && (timer_focus == null))
            {
                timer_focus = new System.Timers.Timer();
                timer_focus.Interval = 500;
                timer_focus.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
                timer_focus.Enabled = true;
            }
            if ((foodspec != null) && (foodspec.weighType == (int)GlobalEnumVars.WeighType.Weigh))
                lblScaleQty.Type = UITextBox.UIEditType.String;
            else
                lblScaleQty.Type = UITextBox.UIEditType.Integer;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
               this.lblScaleQty.Text = Program.frmOrdering.lblScaleQty.Text;
            }
            catch { }
        }

        private void txtQuantity_GotFocus(object sender, System.EventArgs e)
        {
            if ((foodspec != null) && (foodspec.weighType == (int)GlobalEnumVars.WeighType.Weigh))
                numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 4);
            else
               numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Integer);
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            try
            {
                if (foodspec == null)
                {
                    ShowInfoTip("foodspec is null");
                }
                this.lblScaleQty.Text = ordering.quantity.ToString("0.##");
                if ((foodspec != null) && (foodspec.weighType == (int)GlobalEnumVars.WeighType.Weigh))
                {
                    //公斤转换成斤
                    if (AppConstVars.mConvert == 2 && AppConstVars.mScaleType > 0)
                    {
                        lblScaleQty.Width = pnlAmount.Width - pnlScaleQty.Width - 2;
                        pnlScaleQty.Left = lblScaleQty.Left + lblScaleQty.Width + 1;
                        pnlScaleQty.Visible = true;
                    }
                    if (AppConstVars.mScaleType > 0)
                    {
                        this.pnlQtyName.Text = "重量";
                        this.pnlTitle.Text = "商品取重";
                        (this.Owner as FMainWin).setWeighQty += A_SetWeighQty;                        
                        //this.lblScaleQty.Text = Program.frmMainWin.lblScaleQty.Text;
                        //A_SetWeighQty(CommFunc.ConvertToDecimal(this.lblScaleQty.Text));
                        specName1.Visible = true;
                        specName2.Visible = true;
                        specName3.Visible = true;                        
                    }
                }

                specName1.Text = AppConstVars.systemsetup.specName1.ObjToString();
                specName2.Text = AppConstVars.systemsetup.specName2.ObjToString();
                specName3.Text = AppConstVars.systemsetup.specName3.ObjToString();

                lblFoodName.Text = ordering.name;
                txtSpecName.Text = ordering.specName;
                lblPrice.Text = ordering.realPrice.ToString();                               
            }
            catch { }
        }

        public void A_SetWeighQty(decimal dQty)
        {
            A_SetLog(dQty.ToString());
            if ((foodspec != null) && (foodspec.weighType.ObjToInt() == (int)GlobalEnumVars.WeighType.Weigh))
            {
                if (AppConstVars.mConvert == 2 && AppConstVars.mScaleType > 0)
                {
                    pnlScaleQty.Text = (dQty / 2).ToString() + "公斤";
                    lblScaleQty.Text = dQty.ToString();
                }
                else
                {
                    lblScaleQty.Text = dQty.ToString();
                }
            }
        }

        public void A_SetLog(string msg)
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    if (this.rtb_log.Text.Length > 5000) rtb_log.Text = String.Empty;
                    this.rtb_log.AppendText(msg);
                    this.rtb_log.HideSelection = false;
                }));
            }
            catch (Exception ex) { }
        }

        private bool CheckValues()
        {
            if (CommFunc.ConvertToDecimal(this.lblScaleQty.Text) <= 0)
            {
                UIMessageBox.Show("没有录入正确的数量值。", "出错:", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            mScaleQty = CommFunc.ConvertToDecimal(this.lblScaleQty.Text);
            if ((mScaleQty > ordering.limitBuyQty) && (ordering.limitBuyQty > 0))
            {
                ShowInfoTip("已超过限购数量,无法提交修改");
                return false;
            }
            if ((mScaleQty > 9999999) || (mScaleQty < -999999))
            {
                ShowInfoTip("已超过数量范围,无法提交。");
                return false;
            }
            decimal value = CommFunc.ConvertToDecimal(this.lblAmount.Text);
            if ((value > 99999999) || (value < -99999999))
            {
                ShowInfoTip("已超过金额范围,无法提交。");
                return false;
            }
            return true;
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            if (!CheckValues()) return;
            this.DialogResult = DialogResult.OK;
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            lblAmount.Text = (ordering.realPrice * CommFunc.ConvertToDecimal(lblScaleQty.Text)).ToString("0.00#");
        }

        private void frmFoodSpecWeigh_Shown(object sender, EventArgs e)
        {
            this.lblScaleQty.Focus();
            this.lblScaleQty.SelectionStart = 0;
            this.lblScaleQty.SelectionLength = 100;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void uiPanel3_DoubleClick(object sender, EventArgs e)
        {
            if (lblScaleQty.Text == "609") this.Width = 1200;
        }

        private void specName1_Click(object sender, EventArgs e)
        {
            string specName = (sender as UIButton).Text;
            if (!string.IsNullOrEmpty(specName)) txtSpecName.Text = specName;
            this.lblScaleQty.Focus();
            this.lblScaleQty.SelectionStart = 0;
            this.lblScaleQty.SelectionLength = 100;
        }

        private void frmFoodSpecWeigh_FormClosed(object sender, FormClosedEventArgs e)
        {
            (this.Owner as FMainWin).setWeighQty -= A_SetWeighQty;
        }

        private void lblScaleQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                string str = lblScaleQty.Text;
                int idx = lblScaleQty.SelectionStart;
                str = str.Insert(idx, ".");
                lblScaleQty.Text = str;
                lblScaleQty.SelectionStart = idx + 1;
                lblScaleQty.Focus();
                e.Handled = true;
            }
        }
    }
}
